package com.nondo.dean.product.service;



import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.product.domain.ProductMix;
import com.nondo.dean.product.domain.ProductMixExample;
import com.nondo.dean.product.domain.ProductWithBLOBs;
import com.nondo.dean.product.mapper.ProductExtendsMapper;
import com.nondo.dean.product.mapper.ProductMixMapper;
import com.nondo.dean.product.model.AppProductBean;
import com.nondo.dean.product.model.result.ProductMixResult;
import com.nondo.dean.product.model.result.ProductResult;

/**
 * 产品组合管理业务类
 * @author nondo
 * @date 2018年6月25日
 */
@Component
public class ProductMixService {
    
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductMixMapper  productMixMapper;
    
    @Autowired
    private ProductExtendsMapper productExtendsMapper;


    /**
     * 插入产品组合
     * @param productMix
     * @return
     */
    @Transactional
    public int addProductMix(ProductMix productMix) {
        return productMixMapper.insertSelective(productMix);
    }

    /**
     * 修改产品组合
     * @param productMix
     * @return
     */
    @Transactional
    public int updateProductMix(ProductMix productMix) {
        return productMixMapper.updateByPrimaryKeySelective(productMix);
    }

    /**
     * 按照主键ID查询产品组合
     * @param id
     * @return
     */
    public ProductMix selectProductMix(Integer id) {
        return productMixMapper.selectByPrimaryKey(id);
    }

    /**
     * 产品组合分页查询列表
     * @param offset
     * @param limit
     * @param productMix
     * @return
     */
    public List<ProductMix> getProductMixList(int offset, int limit, ProductMix productMix) {
        ProductMixExample example = new ProductMixExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        ProductMixExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, productMix);
        return productMixMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 产品组合总数统计
     * @param productMix
     * @return
     */
    public Long count(ProductMix productMix) {
        ProductMixExample example = new ProductMixExample();
        ProductMixExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, productMix);
        return productMixMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param productMix
     */
    void setCriteria(ProductMixExample.Criteria criteria, ProductMix productMix) {
        if (productMix != null) {
            if (productMix.getId() != null)
                criteria.andIdEqualTo(productMix.getId());
            if (StringUtils.isNoneBlank(productMix.getMixName()))
                criteria.andMixNameLike("%" + productMix.getMixName() + "%");
            if (productMix.getShelfStatus() != null)
                criteria.andShelfStatusEqualTo(productMix.getShelfStatus());
        }
        criteria.andDelFlagEqualTo(ProductMix.DEL_FLAG_NORMAL);
    }

   /**
    * 按照产品组合名称查询产品组合
    * @param name
    * @return
    */
    public ProductMix selectProductMixByName(String name) {
        ProductMixExample example = new ProductMixExample();
        ProductMixExample.Criteria criteria = example.createCriteria();
        criteria.andMixNameEqualTo(name);
        criteria.andDelFlagEqualTo(ProductMix.DEL_FLAG_NORMAL);
        List<ProductMix> productMixList = productMixMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(productMixList)?productMixList.get(0):null;
    }

    /**
     * 获取产品组合明细中的产品列表
     * @param productMix
     * @return {@link ProductMixResult}
     */
    public ProductMixResult getProductMixResult(ProductMix productMix) {
        ProductMixResult productMixResult = ProductMixResult.covertToBean(productMix);
        if (StringUtils.isEmpty(productMix.getProductIds())) {
            return productMixResult;
        }
        BigDecimal mixPrice = new BigDecimal(0.00);
        List<ProductResult> productList = new ArrayList<ProductResult>();
        for (String prodcutId : productMix.getProductIds().split(",")) {
            ProductWithBLOBs product = productService.selectById(Integer.parseInt(prodcutId));
            ProductResult productResult = ProductResult.covertToBean(product);
            mixPrice = mixPrice.add(productResult.getSalePrice());
            productList.add(productResult);
        }
        if (mixPrice.equals(productMixResult.getMixPrice())) {
            productMixResult.setMixPrice(mixPrice);
        }
        productMixResult.setProductList(productList);
        return productMixResult;
    }
    
    /**
     * 查找产品所参与的产品组合的产品列表
     * @param productId
     * @return List<{@link AppProductBean}>
     */
    public List<ProductMixResult> getProductMixListByProductId(Integer productId) {
        List<ProductMixResult> productMixResultList = new ArrayList<ProductMixResult>();
        List<ProductMix> productMixList = this.productExtendsMapper.getProductMixListByProductId(productId);
        for (ProductMix productMix : productMixList) {
            ProductMixResult productMixResult = getProductMixResult(productMix);
            productMixResultList.add(productMixResult);
        }
        return productMixResultList;
    }
    
    /**
     * 查找产品所参与的产品组合列表
     * @param productId
     * @return
     */
    public List<ProductMix> getProductMixList(Integer productId) {
        List<ProductMix> productMixList = this.productExtendsMapper.getProductMixListByProductId(productId);
        return productMixList;
    }
}
